package com.sls.mall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.sls.mall.product.vo.AttrGroupRelationVO;
import com.sls.mall.product.vo.AttrRespVO;
import com.sls.mall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sls.mall.product.entity.AttrEntity;
import com.sls.mall.product.service.AttrService;
import com.sls.mall.common.utils.PageUtils;
import com.sls.mall.common.utils.R;



/**
 * 商品属性
 *
 * @author tg
 * @email tg@gmail.com
 * @date 2021-04-24 16:14:45
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String,Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type)
    {
        PageUtils pageUtils = attrService.queryBaseAttrPage(params,catelogId,type);

        return R.ok().put("page",pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVO attrRespVO = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrRespVO);
    }

//    /**
//     * 保存
//     */
//    @RequestMapping("/save")
//    public R save(@RequestBody AttrEntity attr){
//		attrService.save(attr);
//
//        return R.ok();
//    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attr){
        attrService.saveAttr(attr);

        return R.ok();
    }

//    /**
//     * 修改
//     */
//    @RequestMapping("/update")
//    public R update(@RequestBody AttrEntity attr){
//		attrService.updateById(attr);
//
//        return R.ok();
//    }


    /**
    *   修改
    */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attr){
//		attrService.updateById(attr);

        attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }



}
